package com.hibernate.daoimp.news;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hibernate.dao.news.ArticlesLeibieServiceDao;
import com.hibernate.pojo.ArticlesLeibie;
import com.struts.form.ArticlesLeibieForm;
import com.common.Constant;


public class ArticlesLeibieServiceDaoImp extends HibernateDaoSupport implements ArticlesLeibieServiceDao{
	
	private boolean bool;
	private List list;
	
	public boolean saveNewsLeibie(ArticlesLeibie articlesleibie){
		bool=false;
		try
		{
			this.getHibernateTemplate().save(articlesleibie);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	
	public boolean updateNewsLeibie(ArticlesLeibieForm articlesleibieform){
		try
		{
			ArticlesLeibie articleleibie=(ArticlesLeibie)this.getHibernateTemplate().load(ArticlesLeibie.class, articlesleibieform.getId());
			articleleibie.setLeibie(articlesleibieform.getLeibie());
			this.getHibernateTemplate().update(articleleibie);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * delete article leibie
	 */
	public boolean deleteNewsLeibie(Long id){
		try
		{
			//init List
			list=new ArrayList();
			//load will delete type
			ArticlesLeibie articlesleibie=(ArticlesLeibie)this.getHibernateTemplate().load(ArticlesLeibie.class, id);
			list.add(articlesleibie);
			//delete related articles
			final Long artleibie=articlesleibie.getId();
			this.getHibernateTemplate().execute(new HibernateCallback()
			{
				public Object doInHibernate(Session session)throws HibernateException, SQLException
				{
					Query query=session.createQuery("delete from Articles where leibie=:leibie");
					query.setLong("leibie",artleibie);
					int n=query.executeUpdate();
					return n;
				}
			});
			//call recursion delete
			List collection=deleteRecursion(id);
			//delete type and child type
			this.getHibernateTemplate().deleteAll(collection);
			bool=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			bool=false;
		}
		return bool;
	}
	
	/**
	 * Recursion delete child type , use stack
	 * @param id
	 * @param link
	 */
	public List deleteRecursion(Long id)
	{
		ArticlesLeibie articlesleibie=null;
		List collection=new ArrayList();
		collection=this.getHibernateTemplate().find("from ArticlesLeibie where fatherId="+id);
		if(collection.size()>0)
		{
			for(int i=0;i<collection.size();i++)
			{
				articlesleibie=(ArticlesLeibie)collection.get(i);
				list.add(articlesleibie);
				//delete related articles
				final Long artleibie=articlesleibie.getId();
				this.getHibernateTemplate().execute(new HibernateCallback()
				{
					public Object doInHibernate(Session session)throws HibernateException, SQLException
					{
						Query query=session.createQuery("delete from Articles where leibie=:leibie");
						query.setLong("leibie",artleibie);
						int n=query.executeUpdate();
						return n;
					}
				});
				//call recursion
				deleteRecursion(articlesleibie.getId());
			}
				
		}
		return list;
	}
	
	
	public List findNewsLeibie(String hql){
		list=this.getHibernateTemplate().find(hql);
		return list;
	}
}
